package com.imageim.yimirror.common.data;

import android.util.Log;

import com.imageim.yimirror.bean.TestQuestionBean;
import com.imageim.yimirror.common.util.LogUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 使用说明： When the user choose left picture then invoke TestTreeNode.getLeftNode() to get the next question data
 * , otherwise invoke TestTreeNode.getRightNode().
 * Created by Administrator on 2018/3/5.
 */

public class TestTree {

    private static List<TestTreeNode> nodeList = new ArrayList<>();

    private static int nodeIndex; // 节点索引，从0开始，从上到下，从左到右
    private static List<TestQuestionBean.ListBean> mList;

    /**
     * 数组长度必须是126
     */
//    private static final int[] imageResIds = new int[]{
//            R.drawable.test_question_icon_1_sweet, R.drawable.test_question_icon_1_cool,
//            R.drawable.test_question_icon_2_sweet, R.drawable.test_question_icon_2_modern, R.drawable.test_question_icon_2_romance, R.drawable.test_question_icon_2_cool,
//            R.drawable.test_question_icon_3_sweet, R.drawable.test_question_icon_3_cool, R.drawable.test_question_icon_3_romance, R.drawable.test_question_icon_3_modern, R.drawable.test_question_icon_3_romance2, R.drawable.test_question_icon_3_modern2, R.drawable.test_question_icon_3_sweet, R.drawable.test_question_icon_3_cool,
//            R.drawable.test_question_icon_4_sweet, R.drawable.test_question_icon_4_romance, R.drawable.test_question_icon_4_cool, R.drawable.test_question_icon_4_modern, R.drawable.test_question_icon_4_sweet, R.drawable.test_question_icon_4_romance, R.drawable.test_question_icon_4_cool, R.drawable.test_question_icon_4_modern, R.drawable.test_question_icon_4_sweet, R.drawable.test_question_icon_4_romance, R.drawable.test_question_icon_4_cool, R.drawable.test_question_icon_4_modern, R.drawable.test_question_icon_4_sweet, R.drawable.test_question_icon_4_romance, R.drawable.test_question_icon_4_cool, R.drawable.test_question_icon_4_modern,
//            R.drawable.test_question_icon_5_sweet, R.drawable.test_question_icon_5_classical, R.drawable.test_question_icon_5_romance, R.drawable.test_question_icon_5_classical, R.drawable.test_question_icon_5_cool, R.drawable.test_question_icon_5_classical, R.drawable.test_question_icon_5_modern, R.drawable.test_question_icon_5_classical, R.drawable.test_question_icon_5_sweet, R.drawable.test_question_icon_5_classical2, R.drawable.test_question_icon_5_romance, R.drawable.test_question_icon_5_classical2, R.drawable.test_question_icon_5_cool, R.drawable.test_question_icon_5_classical2, R.drawable.test_question_icon_5_modern, R.drawable.test_question_icon_5_classical2, R.drawable.test_question_icon_5_sweet, R.drawable.test_question_icon_5_classical3, R.drawable.test_question_icon_5_romance, R.drawable.test_question_icon_5_classical3, R.drawable.test_question_icon_5_cool, R.drawable.test_question_icon_5_classical3, R.drawable.test_question_icon_5_modern, R.drawable.test_question_icon_5_classical3, R.drawable.test_question_icon_5_sweet, R.drawable.test_question_icon_5_classical4, R.drawable.test_question_icon_5_romance, R.drawable.test_question_icon_5_classical4, R.drawable.test_question_icon_5_cool, R.drawable.test_question_icon_5_classical4, R.drawable.test_question_icon_5_modern, R.drawable.test_question_icon_5_classical4,
//            R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_sweet, R.drawable.test_question_icon_6_classical,
//            R.drawable.test_question_icon_6_modern, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_cool, R.drawable.test_question_icon_6_classical, R.drawable.test_question_icon_6_romance, R.drawable.test_question_icon_6_classical
//    };

    public static TestTreeNode create(List<TestQuestionBean.ListBean> list) {
        if (list == null || list.size() != 126) {
            return null;
        }

        mList = list;
        int deep = 0;
        nodeList.clear();
        nodeIndex = 0;
        TestTreeNode rootNode = new TestTreeNode();
        rootNode.setDeep(deep);
        rootNode.setLeftImageUrl(mList.get(nodeIndex).getPhotoUrl());
        rootNode.setRightImageUrl(mList.get(nodeIndex + 1).getPhotoUrl());

        rootNode.setLeftQuestionId(mList.get(nodeIndex).getId());
        rootNode.setLeftStyleValue(mList.get(nodeIndex).getStyleValue());
        rootNode.setRightQuestionId(mList.get(nodeIndex + 1).getId());
        rootNode.setRightStyleValue(mList.get(nodeIndex + 1).getStyleValue());

        nodeIndex += 2;
        nodeList.add(rootNode);
        for (int i = 0; i < 5; i++) {
            List<TestTreeNode> newNodeList = new ArrayList<>();
            for (TestTreeNode node : nodeList) {
                if (node.getDeep() == deep) {
                    TestTreeNode leftNode = new TestTreeNode();
                    TestTreeNode rightNode = new TestTreeNode();
                    leftNode.setDeep(deep + 1);
                    rightNode.setDeep(deep + 1);
                    leftNode.setLeftImageUrl(mList.get(nodeIndex).getPhotoUrl());
                    leftNode.setRightImageUrl(mList.get(nodeIndex + 1).getPhotoUrl());

                    leftNode.setLeftQuestionId(mList.get(nodeIndex).getId());
                    leftNode.setLeftStyleValue(mList.get(nodeIndex).getStyleValue());
                    leftNode.setRightQuestionId(mList.get(nodeIndex + 1).getId());
                    leftNode.setRightStyleValue(mList.get(nodeIndex + 1).getStyleValue());

                    nodeIndex += 2;
                    rightNode.setLeftImageUrl(mList.get(nodeIndex).getPhotoUrl());
                    rightNode.setRightImageUrl(mList.get(nodeIndex + 1).getPhotoUrl());

                    rightNode.setLeftQuestionId(mList.get(nodeIndex).getId());
                    rightNode.setLeftStyleValue(mList.get(nodeIndex).getStyleValue());
                    rightNode.setRightQuestionId(mList.get(nodeIndex + 1).getId());
                    rightNode.setRightStyleValue(mList.get(nodeIndex + 1).getStyleValue());

                    nodeIndex += 2;
                    node.setLeftNode(leftNode);
                    node.setRightNode(rightNode);
                    newNodeList.add(leftNode);
                    newNodeList.add(rightNode);
                }
            }
            deep++;
            nodeList.addAll(newNodeList);
        }
        // logs
        LogUtil.d("tree size: ", "size=" + nodeList.size());
        //        for (TestTreeNode node : nodeList) {
        //            Log.d("node ", "node -> deep=" + node.getDeep()
        //                    + ", left=" + node.getLeftImageUrl() + ", right=" + Integer.toHexString(node.getRightImageUrl()) + "\n");
        //        }
        return rootNode;
    }

    /**
     * 获得第几层的第几个节点，(防止后台配置错误，不认账~ 或测试用)
     * @param deep
     * @param col
     * @return
     */
    public static TestTreeNode getNode(int deep, int col) {
        int nodeNumInThisDeep = (int)Math.pow(2, deep);
        if (deep < 0 || deep > 5 || col < 0 || col >= nodeNumInThisDeep) {
            Log.d("TestTree", "Bad params");
            return null;
        }
        int currCol = 0;
        for (TestTreeNode node : nodeList) {
            if (node.getDeep() == deep) {
                if (currCol == col) {
                    Log.d("TestTree", "pics = " + node.getRightImageUrl() + "," + node.getRightImageUrl());
                    return node;
                }
                currCol++;
            }
        }
        return null;
    }
}
